<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" lay-filter="userEditForm">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" autocomplete="off" placeholder="请输入账号" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text"  name="realName" lay-verify="required" autocomplete="off" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text"  name="email" lay-verify="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
                    </div>
                </div>
               <%-- <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password"  name="password" lay-verify="pass" autocomplete="off" placeholder="请输入密码" class="layui-input">
                    </div>
                </div>--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text"  name="phone" lay-verify="phone" autocomplete="off" placeholder="请输入电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                            <input type="radio" name="sex" value="1" title="男" checked>
                            <input type="radio" name="sex" value="0" title="女">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <select name="roleIds" xm-select="roles" xm-select-skin="default" lay-verify="required">
                            <c:forEach items="${roleList}" var="role">
                                <option value="${role.roleId}">${role.roleName}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['form','jquery','select'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    let select = layui.select;
    let userId
    form.on('submit(user-save)', function(data){
        data.field.userId = userId
        $.ajax({
            url:'/user/create',
            data:JSON.stringify(data.field),
            dataType:'json',
            contentType:'application/json',
            type:'post',
            success:function(){
                layer.msg('用户修改成功',{icon:1,time:1000},function(){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.layui.table.reload("user-table");
                });
            },
            error: function (data) {
                layer.msg(data.responseJSON.detail,{icon:2,time:1000});
            }
        })
        return false;
    });
    //表单校验规则
    form.verify({
        pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
    });
    /**
     * 编辑表单初始化回显
     * @param obj
     */
    window.init = function (obj){
        console.log(obj);//获取父界面的传值
        form.val("userEditForm", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
            "username": obj.username,
            "realName": obj.realName,
            "email": obj.email,
            "phone": obj.phone,
            "sex": obj.sex
        });
        //角色多选回显
        select.value('roles',obj.roleIds.split(','))
        userId = obj.userId;
    }
})
</script>
<script>
</script>
</body>
</html>
